package com.bangyu.monitor.modular.system.model;

import com.bangyu.monitor.core.cache.BridgeCache;
import com.bangyu.monitor.core.common.constant.Const;
import com.bangyu.monitor.core.common.constant.ConstantEnum;
import com.bangyu.monitor.core.util.BendCacuUtil;
import com.bangyu.monitor.modular.system.entity.OriginalData;
import com.bangyu.monitor.modular.system.entity.Payload;
import com.bangyu.monitor.modular.system.entity.StaticStandDisplayment;
import com.bangyu.monitor.modular.system.entity.WeightPayload;
import com.google.common.collect.Lists;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.time.DateFormatUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * @Auther: zhangxue
 * @Date: 19/4/24 15:24
 * @Description:
 */
@Data
public class OriginalDataDto {

    private String deviceNo;
    private long receiveTime;
    private Float v0;
    private Float v1;
    private Float v2;
    private Float v3;
    private Float v4;
    private Float v5;
    private Float v6;
    private Float v7;

    //标记作用，如果该数据为重型荷载，
    public boolean recover;
    public Date recoverTime;



    public OriginalDataDto buildDeviceNo(String deviceNo){
        this.setDeviceNo(deviceNo);
        return this;
    }


    public OriginalDataDto buildV1(Float v1){
        this.setV1(v1);
        return this;
    }

    public OriginalDataDto buildV0(Float v0){
        this.setV0(v0);
        return this;
    }

    public OriginalDataDto buildV2(Float v2){
        this.setV2(v2);
        return this;
    }

    public OriginalDataDto buildV3(Float v3){
        this.setV3(v3);
        return this;
    }

    public OriginalDataDto buildV4(Float v4){
        this.setV4(v4);
        return this;
    }
    public OriginalDataDto buildV5(Float v5){
        this.setV5(v5);
        return this;
    }
    public OriginalDataDto buildV6(Float v6){
        this.setV6(v6);
        return this;
    }
    public OriginalDataDto buildV7(Float v7){
        this.setV7(v7);
        return this;
    }

    public OriginalDataDto buildStandBendValue(StaticStandDisplayment staticStandDisplayment){
        return this;
    }
    public OriginalDataDto buildReceiveTime(long time){
        this.setReceiveTime(time);
        return this;
    }



/*

    //是否为重型荷载
    public boolean isHeavyPayload(){
        BridgeCache.BridgeCacheEntity entity = BridgeCache.getCache(getDeviceNo());
        if(relativeDisplayment == null) return false;
        Float relativeDisplaymentAbs = new BigDecimal(relativeDisplayment).abs().floatValue();
        return relativeDisplaymentAbs.compareTo(entity.getHeavyThreshold())>0;
    }

    //是否为轻型荷载
    public boolean isLightPayload(){
        BridgeCache.BridgeCacheEntity entity = BridgeCache.getCache(getDeviceNo());
        if(relativeDisplayment == null) return false;
        Float relativeDisplaymentAbs = new BigDecimal(relativeDisplayment).abs().floatValue();
        return relativeDisplaymentAbs.compareTo(entity.getLightThreshold())>0 //轻型荷载
                && relativeDisplaymentAbs.compareTo(entity.getHeavyThreshold())<=0;
    }

    //原始数据转成荷载数据
    public Payload toPayload() {

        if(this.isLightPayload()||this.isHeavyPayload()){
            Payload payload = new Payload();
            payload.setBridgeId(this.getBridgeId());
            payload.setBeamNo(this.getBeamNo());
            payload.setDeviceNo(this.getDeviceNo());
            payload.setReceiveTime(this.getReceiveTime());
            payload.setTemperature(this.getTemperature());
            payload.setBendValue(this.getBendValue());
            payload.setRelativeDisplayment(this.getRelativeDisplayment());
            payload.setPayloadValue(this.getPayloadValue());
            payload.setV0(this.getV0());
            return payload;
        }


        return null;
    }

    public WeightPayload toWeightPayload() {
        if(this.isHeavyPayload()){
            WeightPayload weightPayload = new WeightPayload();
            weightPayload.setBridgeId(this.getBridgeId());
            weightPayload.setBeamNo(this.getBeamNo());
            weightPayload.setDeviceNo(this.getDeviceNo());
            weightPayload.setReceiveTime(this.getReceiveTime());
            weightPayload.setTemperature(this.getTemperature());
            weightPayload.setBendValue(this.getBendValue());
            weightPayload.setDisplayment(this.getV0());
            weightPayload.setStaticDisplayment(this.getStaticDisplayment());
            weightPayload.setRelativeDisplayment(this.getRelativeDisplayment());

            return weightPayload;
        }
        return null;
    }

*/


    public static List<OriginalData> toModules(List<OriginalDataDto> dtos){
        if(CollectionUtils.isEmpty(dtos)) return null;
        List<OriginalData> data = Lists.newArrayList();
        for(OriginalDataDto dataDto:dtos){
            OriginalData originalData = new OriginalData();
            originalData.setDeviceNo(dataDto.getDeviceNo());
            originalData.setReceiveTime(dataDto.getReceiveTime());
            originalData.setV0(dataDto.getV0());
            originalData.setV1(dataDto.getV1());
            originalData.setV2(dataDto.getV2());
            originalData.setV3(dataDto.getV3());
            originalData.setV4(dataDto.getV4());
            originalData.setV5(dataDto.getV5());
            originalData.setV6(dataDto.getV6());
            originalData.setV7(dataDto.getV7());
            data.add(originalData);
        }
        return data;
    }

}
